<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-10-19
 * Time: 14:39
 */

namespace app\src\excel\action;


use app\src\base\action\BaseAction;



class ExcelAction extends BaseAction{

    /*
     * 输出excel表格
     * ￥array为表头
     * data为表数据
     * name为表名
     */
    public function outlet($array,$data,$name) {
        $objPHPExcel = new \PHPExcel();
        $objProps    = $objPHPExcel->getProperties();

        $count=count($array);

//        for ($i = 1; $i < $count; $i++) {
//            if ($i > 1) {
//                $objPHPExcel->createSheet();
//            }

        $objPHPExcel->setActiveSheetIndex(0);//把当前创建的sheet设置为活动sheet
        $objSheet = $objPHPExcel->getActiveSheet();//获得当前活动Sheet
        $objSheet->setTitle("user" . 1);


        $cell_array=['A','B','C','D','E','F','G','H','I','J','K','L','M','N'];
        foreach($array as $k=>$v){
            $cell=$cell_array[$k].'1';
            $objSheet->setCellValue($cell,$v);
        }

        $j = 2;
        foreach ($data as $key => $value) {

            foreach($value as $k=>$v){
                $objSheet->setCellValue($cell_array[$k]. $j, $v);
            }
            $j++;
        }
        // }

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename='.$name.'.xls');
        header("Content-Transfer-Encoding:binary");
        $objWriter->save('php://output');
        exit;
    }

}